package com.redsoil.house.ui.property.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.redsoil.house.R;
import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.RootFragment;
import com.redsoil.house.model.bean.OnePropertyVO;
import com.redsoil.house.ui.property.contract.PropertyInfoContract;
import com.redsoil.house.ui.property.presenter.PropertyInfoPresenter;
import com.redsoil.house.utils.Util;
import com.redsoil.house.widget.PropertyInfoItem;

import butterknife.BindView;

/**
 * 物业信息
 * 里面包括 地图信息
 */
public class PropertyInfoFragment extends RootFragment<PropertyInfoPresenter> implements PropertyInfoContract.View {

    @BindView(R.id.buy_price)
    PropertyInfoItem buy_price;

    @BindView(R.id.surface_rate)
    PropertyInfoItem surface_rate;

    @BindView(R.id.build_date)
    PropertyInfoItem build_date;

    @BindView(R.id.build)
    PropertyInfoItem build;

    @BindView(R.id.build_floor)
    PropertyInfoItem build_floor;

    @BindView(R.id.build_area)
    PropertyInfoItem build_area;

    @BindView(R.id.build_tax)
    PropertyInfoItem build_tax;

    @BindView(R.id.manage_cost)
    PropertyInfoItem manage_cost;

    @BindView(R.id.repair_cost)
    PropertyInfoItem repair_cost;

    @BindView(R.id.build_safe)
    PropertyInfoItem build_safe;

    @BindView(R.id.build_address)
    PropertyInfoItem build_address;

    @BindView(R.id.map_view)
    MapView map_view;

//    private MapView mMapView;
//    private BaiduMap mBaiduMap;

    private String propertyId;

    public static PropertyInfoFragment newInstance(String propertyId) {
        Bundle args = new Bundle();
        PropertyInfoFragment fragment = new PropertyInfoFragment();
        fragment.setArguments(args);
        args.putString("propertyId",propertyId);
        return fragment;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();

        propertyId = getArguments().getString("propertyId");

        stateLoading();

        mPresenter.getOneProperty(propertyId);
    }

    @Override
    public void getOnePropertySuccess(OnePropertyVO onePropertyVO) {
        buy_price.setData(getResources().getString(R.string.buy_price),
                Util.nfValue(onePropertyVO.getJbuyPrice())+"万円",
                getResources().getDrawable(R.drawable.shape_top_edge));

        surface_rate.setData(getResources().getString(R.string.surface_rate),
                onePropertyVO.getReturnRate()+"%",
                getResources().getDrawable(R.drawable.shape_top_edge));

        build_date.setData(getResources().getString(R.string.build_date),onePropertyVO.getBuildDate(),getResources().getDrawable(R.drawable.shape_top_edge));
        build.setData(getResources().getString(R.string.build),onePropertyVO.getStructureValue(),getResources().getDrawable(R.drawable.shape_top_edge));
        build_floor.setData(getResources().getString(R.string.build_floor),onePropertyVO.getFloor()+"层",getResources().getDrawable(R.drawable.shape_top_edge));


        if(onePropertyVO.getTypeCode().equals("BUILD")){

            build_area.setData("土地面积",onePropertyVO.getLandArea()+"㎡",getResources().getDrawable(R.drawable.shape_top_edge));

            build_tax.setData("建筑面积",
                    onePropertyVO.getBuildArea()+"㎡",
                    getResources().getDrawable(R.drawable.shape_top_edge));

            manage_cost.setData("建筑率",
                    onePropertyVO.getBuildRate()+"%",
                    getResources().getDrawable(R.drawable.shape_top_edge));

            repair_cost.setData("容积率",
                    onePropertyVO.getPlotRate()+"%",
                    getResources().getDrawable(R.drawable.shape_top_edge));
        }else {
            build_area.setData(getResources().getString(R.string.build_area),onePropertyVO.getArea()+"㎡",getResources().getDrawable(R.drawable.shape_top_edge));

            build_tax.setData(getResources().getString(R.string.build_tax),
                    Util.nfValue(onePropertyVO.getJgdTax())+"円",
                    getResources().getDrawable(R.drawable.shape_top_edge));

            manage_cost.setData(getResources().getString(R.string.manage_cost),
                    Util.nfValue(onePropertyVO.getJmanageCost())+"円",
                    getResources().getDrawable(R.drawable.shape_top_edge));

            repair_cost.setData(getResources().getString(R.string.repair_cost),
                    Util.nfValue(onePropertyVO.getJfixCost())+"円",
                    getResources().getDrawable(R.drawable.shape_top_edge));
        }



        build_safe.setData(getResources().getString(R.string.build_safe),
                onePropertyVO.getInsure()+"",
                getResources().getDrawable(R.drawable.shape_top_edge));

        build_address.setData(getResources().getString(R.string.build_address),
                onePropertyVO.getBuildingAddress(),
                getResources().getDrawable(R.drawable.shape_top_bottom_edge));

        LatLng latLng = new LatLng(onePropertyVO.getLatitude(), onePropertyVO.getLongitude());
        // 设置地图中心点
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory
                .newMapStatus(new MapStatus.Builder().target(latLng)
                        .zoom(18)
                        .build());
        map_view.getMap().setMapStatus(mapStatusUpdate);

        //定义Maker坐标点
//        LatLng point = new LatLng(39.963175, 116.400244);
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_red_point);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(latLng)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        map_view.getMap().addOverlay(option);

//        onePropertyVO.getLatitude() ;
//        onePropertyVO.getLongitude();
        stateMain();
    }


        @Override
    public void onResume() {
        super.onResume();
        if(map_view != null){
            map_view.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(map_view != null){
            map_view.onPause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(map_view != null){
            map_view.onDestroy();
        }
    }

    @Override
    public void responeError(String errorMsg) {
        stateError();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_property_info;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
}
